'''
    Function:
        run.py的函数
'''


'''记录cells的位置并将位置添加到对应的list中'''
def record_cells_info(grid):
    wall_lists = []
    wall_lists_pos = []
    none_lists = []
    none_lists_pos = []
    start_lists = []
    start_lists_pos = []
    end_lists = []
    end_lists_pos = []
    water_lists = []
    water_lists_pos = []
    fire_lists = []
    fire_lists_pos = []
    trans_lists = []
    trans_lists_pos = []
    all_cells_lists = []
    all_cells_lists_pos = []

    for i in range(len(grid)):
        for j in range(len(grid[0])):
            cell = grid[i][j]
            if cell == '*':
                wall_lists.append([i,j,'*'])
                wall_lists_pos.append([i,j])
            elif cell == ' ':
                none_lists.append([i,j,' '])
                none_lists_pos.append([i,j])
            elif cell == 'X':
                start_lists.append([i,j,'X'])
                start_lists_pos.append([i,j])
            elif cell == 'Y':
                end_lists.append([i,j,'Y'])
                end_lists_pos.append([i,j])
            elif cell == 'W':
                water_lists.append([i,j,'W'])
                water_lists_pos.append([i,j])
            elif cell == 'F':
                fire_lists.append([i,j,'F'])
                fire_lists_pos.append([i,j])
            elif cell == '1' :
                trans_lists.append([i,j,'1'])
                trans_lists_pos.append([i,j])
            elif cell == '2' :
                trans_lists.append([i,j,'2'])
                trans_lists_pos.append([i,j])
            elif cell == '3' :
                trans_lists.append([i,j,'3'])
                trans_lists_pos.append([i,j])
            elif cell == '4' :
                trans_lists.append([i,j,'4'])
                trans_lists_pos.append([i,j])
            elif cell == '5' :
                trans_lists.append([i,j,'5'])
                trans_lists_pos.append([i,j])
            elif cell == '6' :
                trans_lists.append([i,j,'6'])
                trans_lists_pos.append([i,j])
            elif cell == '7' :
                trans_lists.append([i,j,'7'])
                trans_lists_pos.append([i,j])
            elif cell == '8' :
                trans_lists.append([i,j,'8'])
                trans_lists_pos.append([i,j])
            elif cell == '9' :
                trans_lists.append([i,j,'9'])
                trans_lists_pos.append([i,j])
    player_lists_row = start_lists[0][0]
    player_lists_col = start_lists[0][1]
    player_lists = [[player_lists_row,player_lists_col,'A']]

    all_cells_lists.append(wall_lists)
    all_cells_lists.append(none_lists)
    all_cells_lists.append(start_lists)
    all_cells_lists.append(end_lists)
    all_cells_lists.append(water_lists)
    all_cells_lists.append(fire_lists)
    all_cells_lists.append(trans_lists)
    all_cells_lists.append(player_lists)

    all_cells_lists_pos.append(wall_lists_pos)
    all_cells_lists_pos.append(none_lists_pos)
    all_cells_lists_pos.append(start_lists_pos)
    all_cells_lists_pos.append(end_lists_pos)
    all_cells_lists_pos.append(water_lists_pos)
    all_cells_lists_pos.append(fire_lists_pos)
    all_cells_lists_pos.append(trans_lists_pos)
    all_cells_lists_pos.append([[player_lists_row,player_lists_col]])

    return all_cells_lists,all_cells_lists_pos

'''将地图上的地点用A覆盖'''
def cover_start_to_A(grid_list_str,player_pos):
    A_row_list = list(grid_list_str[player_pos[0][0]])
    A_row_list[player_pos[0][1]] = player_pos[0][2]
    A_row_list = ''.join(A_row_list)
    grid_list_str[player_pos[0][0]] = A_row_list
    print("### Output ###")
    for grid_list_str_item in grid_list_str:
        print(grid_list_str_item)
    print("")

'''碰撞检测'''
def collision_detection(acron_next_step,wall_lists):
    if acron_next_step in wall_lists:
        # print("撞到墙了")
        return False
    else:
        # print("可以通行")
        return True

'''判断是否可以移动以及怎么移动'''
def check_move(grid_list_str_row,grid_list_str_col,acorn_sprites,move,wall_lists):

    if move == 'w':
        #满足条件后橡子才可以移动
        if acorn_sprites.row > 0 and collision_detection([acorn_sprites.row-1,acorn_sprites.col],wall_lists):
            #更新橡子位置
            acorn_sprites.col_pre = acorn_sprites.col
            acorn_sprites.row_pre = acorn_sprites.row
            acorn_sprites.row -= 1
            acorn_sprites.move_list.append('w')
            return acorn_sprites,False,True
        else:
            print("You walked into a wall. Oof!\n")
            return acorn_sprites,False,False
    #向下按
    elif move == 's':
        if acorn_sprites.row < grid_list_str_row and collision_detection([acorn_sprites.row+1,acorn_sprites.col],wall_lists):
            #更新橡子位置
            acorn_sprites.col_pre = acorn_sprites.col
            acorn_sprites.row_pre = acorn_sprites.row
            acorn_sprites.row += 1
            acorn_sprites.move_list.append('s')
            return acorn_sprites,False,True
        else:
            print("You walked into a wall. Oof!\n")
            return acorn_sprites,False,False
    # 向左按
    elif move == 'a':
        if acorn_sprites.col > 0 and collision_detection([acorn_sprites.row,acorn_sprites.col-1],wall_lists):
            #更新橡子位置
            acorn_sprites.col_pre = acorn_sprites.col
            acorn_sprites.row_pre = acorn_sprites.row
            acorn_sprites.col -= 1
            acorn_sprites.move_list.append('a')
            return acorn_sprites,False,True
        else:
            print("You walked into a wall. Oof!\n")
            return acorn_sprites,False,False
    # 向右按
    elif move == 'd':
        if acorn_sprites.col < grid_list_str_col and collision_detection([acorn_sprites.row,acorn_sprites.col+1],wall_lists):
            #更新橡子位置
            acorn_sprites.col_pre = acorn_sprites.col
            acorn_sprites.row_pre = acorn_sprites.row
            acorn_sprites.col += 1
            acorn_sprites.move_list.append('d')
            return acorn_sprites,False,True
        else:
            print("You walked into a wall. Oof!\n")
            return acorn_sprites,False,False
    #按传送键
    elif move == 'e':
        print("Please do not press 'e' until the transfer point is reached\n")
        return acorn_sprites,False,True
    #按下的是q键，也就是直接退出游戏
    elif move == 'q':
        return acorn_sprites,True,False

'''橡子传送到的地方'''
def player_trans(acorn_sprites,grid_list_str,trans_num):
    for i in range(len(grid_list_str)):
        for j in range(len(grid_list_str[0])):
            rid_str_list_row = list(grid_list_str[i])
            if rid_str_list_row[j] == trans_num:
                trans_pos = [i,j]
    grid_list_str_row = list(grid_list_str[acorn_sprites.row])
    grid_list_str_row[acorn_sprites.col] = trans_num
    grid_list_str_row = ''.join(grid_list_str_row)
    grid_list_str[acorn_sprites.row] = grid_list_str_row

    grid_list_str_row = list(grid_list_str[trans_pos[0]])
    acorn_sprites.row = trans_pos[0]
    acorn_sprites.col = trans_pos[1]
    grid_list_str_row[trans_pos[1]] = 'A'
    grid_list_str_row = ''.join(grid_list_str_row)
    grid_list_str[trans_pos[0]] = grid_list_str_row
    # print("i~~~~~~~~~~~~~:", trans_pos[0])
    for item in grid_list_str:
        print(item)
    print("")
    return acorn_sprites,grid_list_str

def win_lose(acorn_sprites,win_lose,info):
    print(info)
    print("Your made <%d> moves."%len(acorn_sprites.move_list))
    print("Your moves:",acorn_sprites.move_list)
    print("=====================")
    print("====== %s! ====="%win_lose)
    print("=====================")
